package com.api.league.controller;

import com.api.core.annotation.PowerEnable;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultCode;
import com.api.core.response.ResultGenerator;
import com.api.league.enums.LeagueStatus;
import com.api.league.form.CreateLeagueForm;
import com.api.league.model.LeagueCarousel;
import com.api.league.model.LeagueIntroduction;
import com.api.league.service.LeagueCarouselService;
import com.api.league.service.LeagueIntroductionService;
import com.api.league.service.LeagueService;
import com.api.league.vo.LeagueTopPlayerVo;
import com.api.league.vo.LeagueVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

/**
 * @author RenQiang
 * @date 2021/5/26
 */
@Slf4j
@PowerEnable(name = "联赛", url = "/league")
@Api(value = "联赛", tags = {LeagueController.API_TAG})
@RestController
@RequestMapping("/league")
public class LeagueController extends Ctrl {
    public static final String API_TAG = "联赛";

    private final LeagueService leagueService;
    private final LeagueIntroductionService leagueIntroductionService;
    private final LeagueCarouselService leagueCarouselService;

    public LeagueController(LeagueService leagueService, LeagueIntroductionService leagueIntroductionService, LeagueCarouselService leagueCarouselService) {
        this.leagueService = leagueService;
        this.leagueIntroductionService = leagueIntroductionService;
        this.leagueCarouselService = leagueCarouselService;
    }

    @ApiOperation(value = "创建联赛", tags = {API_TAG}, notes = "创建联赛")
    @PostMapping("/create")
    public Result create(@Validated CreateLeagueForm form) {
        leagueService.createLeague(form);
        return ResultGenerator.genCreatedSuccessResult();
    }

    @ApiOperation(value = "查询联赛列表", tags = {API_TAG}, notes = "根据游戏分类查询联赛列表")
    @GetMapping("/list")
    public Result list(@ApiParam(value = "游戏分类ID") @RequestParam Long gameCategoryId,
                       @ApiParam(value = "联赛状态") @RequestParam(required = false, defaultValue = "Processing") LeagueStatus leagueStatus) {
        List<LeagueVo> list = leagueService.findByGameCategoryId(gameCategoryId, leagueStatus);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation(value = "获取联赛资讯", tags = {API_TAG}, notes = "获取联赛资讯")
    @GetMapping("/info")
    public Result info(@ApiParam(value = "联赛ID") @RequestParam Long leagueId) {
        LeagueIntroduction leagueIntroduction = leagueIntroductionService.findBy("leagueId", leagueId);
        return ResultGenerator.genSuccessResult(leagueIntroduction);
    }

    @ApiOperation(value = "删除联赛", tags = {API_TAG}, notes = "删除联赛")
    @GetMapping("/delete")
    public Result delete(@ApiParam(value = "联赛ID") @RequestParam Long leagueId) {
        leagueService.deleteLeague(leagueId);
        return ResultGenerator.genCreatedSuccessResult();
    }

    @ApiOperation(value = "添加轮播海报", tags = {API_TAG}, notes = "添加轮播海报")
    @PostMapping("/add-carousel")
    public Result addCarousel(@ApiParam("轮播图URL") @RequestParam("imageUrl") String imageUrl,
                              @ApiParam("超链接地址") @RequestParam(value = "href", required = false) String href) {
        LeagueCarousel leagueCarousel = new LeagueCarousel();
        leagueCarousel.setImageUrl(imageUrl);
        leagueCarousel.setHref(href);
        leagueCarouselService.save(leagueCarousel);
        return ResultGenerator.genCreatedSuccessResult();
    }

    @ApiOperation(value = "删除轮播海报", tags = {API_TAG}, notes = "删除轮播海报")
    @PostMapping("/delete-carousel")
    public Result deleteCarousel(@ApiParam("轮播图ID") @RequestParam("carouselId") Long carouselId) {
        leagueCarouselService.deleteById(carouselId);
        return ResultGenerator.genDeleteSuccessResult();
    }

    @ApiOperation(value = "删除轮播海报", tags = {API_TAG}, notes = "删除轮播海报")
    @GetMapping("/list-carousels")
    public Result listCarousels() {
        List<LeagueCarousel> leagueCarousels = leagueCarouselService.findAll();
        leagueCarousels.sort(Comparator.comparing(LeagueCarousel::getId));
        return ResultGenerator.genSuccessResult(leagueCarousels);
    }

    @ApiOperation(value = "获取Top玩家", tags = {API_TAG}, notes = "获取Top玩家")
    @GetMapping("/get-top-players")
    public Result getTopPlayers(@ApiParam(value = "联赛ID") @RequestParam Long leagueId) {
        List<LeagueTopPlayerVo> topPlayerVos = leagueService.getTopPlayers(leagueId);
        return ResultGenerator.genSuccessResult(topPlayerVos);
    }

    @ApiOperation(value = "设置Top玩家", tags = {API_TAG}, notes = "设置Top玩家")
    @PostMapping("/update-top-player")
    public Result updateTopPlayer(@ApiParam(value = "联赛ID") @RequestParam Long leagueId,
                                  @ApiParam(value = "排名") @RequestParam Integer rank,
                                  @ApiParam(value = "战队ID") @RequestParam Long teamId,
                                  @ApiParam(value = "玩家ID") @RequestParam Long playerId) {
        leagueService.updateTopPlayer(leagueId, rank, teamId, playerId);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "归档联赛", tags = {API_TAG}, notes = "归档联赛")
    @PostMapping("/archive")
    public Result archiveLeague(@ApiParam(value = "联赛ID") @RequestParam Long leagueId) {
        try {
            leagueService.archiveById(leagueId);
        } catch (IllegalArgumentException e) {
            log.error("归档联赛失败", e);
            return ResultGenerator.genFailResult(ResultCode.FAIL.code(), e.getMessage());
        }
        return ResultGenerator.genSuccessResult();
    }
}
